package com.marsoft.test.controller;

import com.marsoft.test.entity.EffectContEntity;
import com.marsoft.test.entity.YaPinManageEntity;
import com.marsoft.test.exception.DefinitionException;
import com.marsoft.test.service.EffectContService;
import com.marsoft.test.utils.CompareDateUtils;
import com.marsoft.test.utils.ResultMap;
import com.marsoft.test.validation.ValidationGroups;
import com.mysql.cj.util.StringUtils;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class EffectContController {
    /**
      *@author:he
      *@Date:2021/5/13 10:33
      *@Description:有效的一般担保合同
      */
    @Autowired
    private EffectContService tEffectContService;
    @GetMapping("/test/{id}")
    public  ResultMap getEffectCont(@PathVariable("id")Integer id){
        System.out.println(id);
        List<EffectContEntity> effectCont = tEffectContService.getEffectCont(id);

        System.out.println(effectCont);
        return ResultMap.ok().put("200",effectCont);
    }

    @RequestMapping("/effect/test")
    public ResultMap getEffectCont1(@Validated (ValidationGroups.GetEffectCont1.class) @RequestParam Integer id, @RequestParam Integer guarantyContractStatus,
                                    @RequestParam(value = "page",defaultValue = "1") Integer page,
                                    @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,BindingResult result){
        System.out.println(id);
        // 如果页面校验出错
        if(result.hasErrors()){

            // 错误信息
            List<FieldError> list = result.getFieldErrors();

            // 收集错误信息
            Map<String, String> res = list.stream().
                    collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage,(o1, o2)->o1));

            return ResultMap.error().put("msg",res);
        }else{
            List<EffectContEntity> effectCont = tEffectContService.getEffectContByState(id,guarantyContractStatus,page,pageSize);
            return ResultMap.ok().put("200",effectCont);
        }

    }

    @GetMapping("/effect/{id}")
    public String getName(@PathVariable("id")Integer id){
        System.out.println(id);
        String name = tEffectContService.getName(id);
        return name;
    }

    @RequestMapping("/ypcontrol/add")
    public ResultMap addYP(@Validated (ValidationGroups.AddYP.class)YaPinManageEntity tYaPinManageEntity,BindingResult result){
        if(result.hasErrors()){

            // 错误信息
            List<FieldError> list = result.getFieldErrors();

            // 收集错误信息
            Map<String, String> res = list.stream().
                    collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage,(o1, o2)->o1));

            return ResultMap.error().put("msg",res);
        }
        //保险单号不合理
        Integer insuranceNo= tYaPinManageEntity.getInsuranceNo();
        if (insuranceNo.toString().length()!=10){
            throw new DefinitionException("保险单号不合理");
        }
        // 保险到期日不能早于当前时间
        if(CompareDateUtils.compareDate(new Date().toString(),tYaPinManageEntity.getInsuranceEndDate())){

            return ResultMap.error().put("error","保险到期日不能早于当前时间");
        }
        return tEffectContService.addYP(tYaPinManageEntity);


    }

    /**
      *@author:he
      *@Date:2021/5/20 17:41
      *@Description:抵押物详情修改
      */
    @RequestMapping("/ypcontrol/update")
    public ResultMap updateYP(@Validated (ValidationGroups.AddYP.class)YaPinManageEntity tYaPinManageEntity,BindingResult result){
        if(result.hasErrors()){

            // 错误信息
            List<FieldError> list = result.getFieldErrors();

            // 收集错误信息
            Map<String, String> res = list.stream().
                    collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage,(o1, o2)->o1));

            return ResultMap.error().put("msg",res);
        }
        return tEffectContService.updateYP(tYaPinManageEntity);


    }
    /**
      *@author:he
      *@Date:2021/5/21 10:15
      *@Description:删除押品信息
      */
    @RequestMapping("/ypcontrol/delete")
    public ResultMap deteleYP(@Validated (ValidationGroups.AddYP.class)YaPinManageEntity tYaPinManageEntity,BindingResult result){
        if(result.hasErrors()){

            // 错误信息
            List<FieldError> list = result.getFieldErrors();

            // 收集错误信息
            Map<String, String> res = list.stream().
                    collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage,(o1, o2)->o1));

            return ResultMap.error().put("msg",res);
        }
        return tEffectContService.deleteYP(tYaPinManageEntity);

    }
}
